Byte Unit
A library for interaction with units of bytes. The units are B for 1 byte, KB for 1000 bytes, KiB for 1024 bytes, MB for 1000000 bytes, MiB for 1048576 bytes, etc, and up to ZiB which is 1180591620717411303424 bytes.
The data type for storing the size of bytes is u128
by default, but can also be changed to u64
by disabling the default features (it will also cause the highest supported unit down to PiB).
Usage
Macros
There are n_*_bytes
macros can be used. The star *
means the unit. For example, n_gb_bytes
can be used to get a n-GB value in bytes.
let result = n_gb_bytes!;
assert_eq!;
You may need to assign a primitive type if the n
is not an integer.
let result = n_gb_bytes!;
assert_eq!;
Byte
The Byte
structure can be used for representing a size of bytes.
The from_str
associated function can parse any SIZE string and return a Byte
instance in common usage. The format of a SIZE string is like "123", "123KiB" or "50.84 MB".
use Byte;
let result = from_str.unwrap;
assert_eq!;
You can also use the from_bytes
and from_unit
associated functions to create a Byte
instance.
use Byte;
let result = from_bytes;
assert_eq!;
use ;
let result = from_unit.unwrap;
assert_eq!;
AdjustedByte
To change the unit of a Byte
instance, you can use the get_adjusted_unit
method.
use ;
let byte = from_str.unwrap;
let adjusted_byte = byte.get_adjusted_unit;
assert_eq!;
To change the unit of a Byte
instance automatically and appropriately, you can use the get_appropriate_unit
method.
use Byte;
let byte = from_bytes;
let adjusted_byte = byte.get_appropriate_unit;
assert_eq!;
use Byte;
let byte = from_bytes;
let adjusted_byte = byte.get_appropriate_unit;
assert_eq!;
The number of fractional digits created by the to_string
method of a AdjustedByte
instance is 2
unless the ByteUnit
is B
.
To change the number of fractional digits in the formatted string, you can use the format
method instead.
use Byte;
let byte = from_bytes;
let adjusted_byte = byte.get_appropriate_unit;
assert_eq!;
No Std
Disable the default features to compile this crate without std.
[]
= "*"
= false
= ["u128"]
Serde Support
Enable the serde
feature to support the serde framework.
[]
= "*"
= ["serde"]
Crates.io
https://crates.io/crates/byte-unit